﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BasicDataManagement;
using System.Xml.Linq;

namespace Cashier
{
    /// <summary>
    /// BillHistoryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BillHistoryWindow : Window
    {
        IOpertation op;
        string cashierIP;
        List<OrderWindow.BillHistory> historyList;
        List<DataModel.HistoryBill> historyBillList;
        int index;
        public BillHistoryWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
        }
        public BillHistoryWindow(List<OrderWindow.BillHistory> historyList, string cashierip)
            : this()
        {
            this.historyList = historyList;
            //index = historyList.Count - 1;
            this.cashierIP = cashierip;
            historyBillList = op.LoadHistoryBills(cashierip);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (historyBillList == null)
            {
                btnGoBackward.IsEnabled = false;
                btnGoForward.IsEnabled = false;
                this.Title = "无今日历史订单";
                return;
            }
            if (historyBillList.Count == 0)
            {
                btnGoBackward.IsEnabled = false;
                btnGoForward.IsEnabled = false;
                this.Title = "无今日历史订单";
                return;
            }
            index = historyBillList.Count - 1;
            ShowBillInfo(index);
            //if (index != -1)
            //{
            //    listViewBill.ItemsSource = new ObservableCollection<OrderWindow.BillItemHistory>(historyList[index].BillItems);
            //}
        }

        private void btnGoBackward_Click(object sender, RoutedEventArgs e)
        {
            index--;
            if (index < 0)
            {
                index = 0;
            }
            //listViewBill.ItemsSource = new ObservableCollection<OrderWindow.BillItemHistory>(historyList[index].BillItems);
            ShowBillInfo(index);
        }

        private void btnGoForward_Click(object sender, RoutedEventArgs e)
        {
            index++;
            if (index >= historyBillList.Count)
            {
                index = historyBillList.Count - 1;
            }
            //listViewBill.ItemsSource = new ObservableCollection<OrderWindow.BillItemHistory>(historyList[index].BillItems);
            ShowBillInfo(index);
        }

        private void ShowBillInfo(int index)
        {
            var tmpBill = historyBillList[index];
            decimal totalPrice = 0.0m;
            foreach (var t in tmpBill.Items)
            {
                totalPrice += t.DishPrice;
            }
            billInfo.Text = "单号：" + tmpBill.BillID +
                "  时间：" + tmpBill.BillTime +
                "  收银员：" + tmpBill.CashierJobNum +
                "\n总价：" + totalPrice.ToString("F2") + "  折扣：" + tmpBill.Discount.ToString("F2") +
                "  餐处：" + tmpBill.EatPlace;
            listViewBill.ItemsSource = new ObservableCollection<DataModel.HistoryBill.HistoryBillItem>(tmpBill.Items);
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
